package com.bbs.web.controller;

import com.bbs.domain.Article;
import com.bbs.domain.Comment;
import com.bbs.entity.Result;
import com.bbs.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @ClassName ArticleController
 * @Description TODO
 * @Author geci
 * @Date 2019/9/22 15:04
 * @Version 1.0
 */

@RestController
@RequestMapping("/web/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 帖子新增
     *
     * @param article
     * @param zoneId
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Article article, String zoneId) {
        article.setSendername("username");
        return articleService.add(article, zoneId);
    }

    /**
     * 根据id查询单个帖子
     *
     * @param articleid 帖子id
     * @return
     */
    @GetMapping("/findById")
    public Article findById(Integer articleid) {
        return articleService.findById(articleid);
    }





    /**
     * @return java.lang.Integer
     * @author chen hong wei
     * @Description 根据用户名查询提交的帖子数量
     * @Date 19:20 2019/9/22
     * @Param [session]
     **/
    @GetMapping( "/findCountByArticle")
    public Integer findArticleByUser( HttpSession session) {
        String username = (String) session.getAttribute("username");
//        String username="admin";
        if (username == null) {
            return null;
        }
        int count = articleService.findCountByUsername(username);
//        System.out.println(count);
        return null;
    }

    /**
     * 查询当天发帖数
     * @return
     */
    @GetMapping("/findArticleByDate")
    public Integer AllPostsTodayiml(){
        int allPostsToday = articleService.AllPostsToday();
//        System.out.println(allPostsToday);
        return allPostsToday;
    }

    /**
     * 根据zoneId查询的帖子
     * @param zoneId 帖子所在区
     * @return
     */
    @GetMapping("/findArticleListByZoneId")
    private List<Article> findArticleListByZoneId(Integer zoneId){
        List<Article> articleList = articleService.findArticleListByZoneId(zoneId);
        return articleList;
    }
    /**
     * 查询所有帖子数
     * @return
     */
    @GetMapping("/findArticleAllPosts")
    public Integer findArticleAllPosts(){
        List<Article> all = articleService.findAll();
        int size = all.size();
        return size;
    }

    /**
     *
     * @param keyword
     * @return
     */
    @GetMapping("/findArticleByKeyword")
    private List<Article> findArticleByKeyword(String keyword){

        //get请求解决中文乱码
        if (!StringUtils.isEmpty(keyword)) {
            try {
                keyword = new String(keyword.getBytes("ISO8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return articleService.findArticleByKeyword(keyword);

    }
}
